(function (window) {
	'use strict';
	//1引用angular
	//2 写angular代码
	//3 创建模块
	//4 创建控制器
	//5 验证你的angular代码的正确创建
	 var app=angular.module('todoApp',[
	 	'todoApp.directive',
		 'todoApp.service']);
	 app.controller('todoCtrl',['$scope','$filter','storageService',function ($scope,$filter,storageService) {
	 	//测试
		// $scope.name='hello todo';
		// console.log(storageService.test);
		 // 查看 增加 删除 修改
		 //查看 初始化数据
		 //todo显示的列表
		 $scope.todoList=storageService.getTodoList();
		 //增加todo
		 //todo的内容
		 $scope.todoText='';

		 //添加todo的方法
		 $scope.addTodo=function () {
		 	//判断$scope.todoText为空不添加数据
			 if($scope.todoText.length>0){
				 //添加数据到todoList中
				 storageService.addTodo($scope.todoText);
				// $scope.todoList.push({id:getID(),text:$scope.todoText,status:false});
				 //添加完数据以后清空 $scope.todoText
				 $scope.todoText='';
			 }
		 };
		 //删除当前todo
		 $scope.delTodo=function (todo) {
		 	//删除todo并且更新todoList
		 	$scope.todoList=storageService.delTodo(todo);
		 	//找到当前的下标
			// var index=$scope.todoList.indexOf(todo);
			 //根据下标删除某一个todo
			// $scope.todoList.splice(index,1);
		 };
		 //修改todo中的内容
		 $scope.editingTodo={};
		 $scope.editTodo=function (todo) {
			 $scope.editingTodo=todo;
		 };
		 //失去焦点保存数据
		 $scope.blurSave=function () {
			 $scope.todoList=storageService.editTodo($scope.todoList);
			 $scope.editingTodo={};
		 };
		 //已未完成数量的统计
		 //console.log($filter('filter')($scope.todoList, {'status': false}));
		 //监视todoList中数据的变换
		 $scope.$watch('todoList',function (newVal,oldVal) {
			$scope.itemLeft=$filter('filter')(newVal, {'status': false}).length;
			 //console.log($filter('filter')(newVal, {'status': false}).length);
			 $scope.toggleAll=!$scope.itemLeft;
			 //显示与隐藏clearCompleted
			 $scope.isShowClearCompleted=$filter('filter')(newVal, {'status': true}).length>0?true:false;
			 //显示与隐藏全选按钮和下面的footer菜单栏
			$scope.isShowFooter=$scope.todoList.length>0?true:false;
		 },true);
		 //清除已完成数据
		 $scope.clearCompleted=function () {
		 	//$scope.todoList=$filter('filter')($scope.todoList,{status:false});
			 $scope.todoList=storageService.clearCompleted();
		 };
		 //全选功能
		 $scope.changeToggleAll=function () {
		 	$scope.todoList=storageService.changeToggleAll($scope.toggleAll);
			// $scope.toggleAll
			 //循环遍历todoList,让当前todoList中的每一个status等于当前toggleAll的值
			 // $scope.todoList.forEach(function (item) {
				//  item.status=$scope.toggleAll;
			 // })
		 };
		 //status的切换
		 $scope.statusFilter={};
		 $scope.changeStatus=function (status) {
			 //console.log(status);
			 switch(status){
				 case 'active':
					 $scope.statusFilter={status:false};
				 	break;
				 case 'completed':
					 $scope.statusFilter={status:true};
				 	break;
				 default:
					 $scope.statusFilter={};
				 	break;
			 }
		 }
	 }])
})(window);
